package com.video.internationalvideodisplayproject.controller;

import com.video.internationalvideodisplayproject.pojo.BlVideoShootReqHd;
import com.video.internationalvideodisplayproject.pojo.vo.ThemeInfoVO;
import com.video.internationalvideodisplayproject.pojo.vo.VideoVO;
import com.video.internationalvideodisplayproject.service.BlVideoShootReqHdService;
import com.video.internationalvideodisplayproject.service.VideoService;
import com.video.internationalvideodisplayproject.utils.AjaxResult;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class videoController {

    @Autowired
    private VideoService videoService;

    /**
     * 获取当前所有视频展示在web页面
     */
    @GetMapping("/getAllVideoDisplay")
    public AjaxResult getAllVideosDisplayInWeb() {
        try {
            List<VideoVO> videos = videoService.getAllVideos();
            if (videos.isEmpty()) {
                return AjaxResult.error("No product video has been uploaded yet.");
            }
            return AjaxResult.success(videos);
        } catch (Exception e) {
            return AjaxResult.error("获取视频列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/videos/category/{category}")
    public AjaxResult getVideosByCategory(@PathVariable String category) {
        try {
            List<VideoVO> videos = videoService.getVideosByCategory(category);
            return AjaxResult.success(videos);
        } catch (Exception e) {
            return AjaxResult.error("获取分类视频失败: " + e.getMessage());
        }
    }

    @GetMapping("/videos/theme/{topicName}")
    public AjaxResult getVideosByTheme(@PathVariable String topicName) {
        try {
            List<VideoVO> videos = videoService.getVideosByTheme(topicName);
            return AjaxResult.success(videos);
        } catch (Exception e) {
            return AjaxResult.error("获取主题视频失败: " + e.getMessage());
        }
    }

    @GetMapping("/videos/search")
    public AjaxResult searchVideos(
            @RequestParam String keyword,
            @RequestParam(required = false) String category) {
        try {
            List<VideoVO> videos;
            if (category != null && !category.trim().isEmpty()) {
                videos = videoService.searchVideosByCategory(category, keyword);
            } else {
                videos = videoService.searchVideos(keyword);
            }
            return AjaxResult.success(videos);
        } catch (Exception e) {
            return AjaxResult.error("搜索视频失败: " + e.getMessage());
        }
    }

    @GetMapping("/videos/statistics")
    public AjaxResult getStatistics() {
        try {
            Map<String, Object> stats = videoService.getVideoStatistics();
            return AjaxResult.success(stats);
        } catch (Exception e) {
            return AjaxResult.error("获取统计信息失败: " + e.getMessage());
        }
    }

    @GetMapping("/videos/categories")
    public AjaxResult getCategories() {
        try {
            List<String> categories = videoService.getAvailableCategories();
            return AjaxResult.success(categories);
        } catch (Exception e) {
            return AjaxResult.error("获取分类列表失败: " + e.getMessage());
        }
    }

//    @GetMapping("/videos/themes")
//    public AjaxResult getThemes() {
//        try {
//            List<String> themes = videoService.getAvailableThemes();
//            return AjaxResult.success(themes);
//        } catch (Exception e) {
//            return AjaxResult.error("获取主题列表失败: " + e.getMessage());
//        }
//    }

    @GetMapping("/themes")
    public AjaxResult getThemes() {
        try {
            List<ThemeInfoVO> themes = videoService.getAvailableThemes();
            // 确保返回的data字段永远不为null
            return AjaxResult.success(themes  != null ? themes : Collections.emptyList());
        } catch (Exception e) {
            return AjaxResult.error(" 获取主题列表失败: " + e.getMessage());
        }
    }

    // 根据主题名称获取视频（而不是themeId）
    @GetMapping("/themes/{themeName}/videos")
    public AjaxResult getThemeVideos(@PathVariable String themeName) {
        try {
            List<VideoVO> videos = videoService.getVideosByThemeName(themeName);
            return AjaxResult.success(videos);
        } catch (Exception e) {
            return AjaxResult.error("获取主题视频失败: " + e.getMessage());
        }
    }
}
